<?php

namespace App\Bac\Controller\RBAC;

use App\Abstract\Controller;
use App\Bac\Handler\RBAC\RoleHandler;
use App\Bac\Request\RBAC\RolesRequest;
use App\Helper\RespHelper;
use Hyperf\Di\Annotation\Inject;

class RolesController extends Controller
{

    #[Inject]
    protected RoleHandler $handler;

    public function index(): array
    {
        $data = $this->handler->getList($this->request->query());
        return RespHelper::success($data);
    }

    public function create()
    {
        $request = make(RolesRequest::class);
        $request->scene('add')->validateResolved();
        $data = $request->all();
        if (!$this->handler->addRoles($data))
            return RespHelper::fail();
        return RespHelper::success();
    }

    public function edit()
    {
        $request = make(RolesRequest::class);
        $request->scene('edit')->validateResolved();
        $data = $request->all();
        $id = $data['id'];
        unset($data['id']);
        if (!$this->handler->editRoles((int)$id, $data))
            return RespHelper::fail();
        return RespHelper::success();
    }

    public function del()
    {
        $ids = $this->request->input('ids');
        if (!$this->handler->delRoles($ids))
            return RespHelper::fail();
        return RespHelper::success();
    }
}
